package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.entity.mg.mistakes.TeaMistakesKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyKnowledgeRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesKnowRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesQuesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 教师查看知识点错误率（根据班级与知识点）
 * 
 * @author Administrator
 *
 */
@CrossOrigin
@RestController
public class TeaMistakesKnowController extends YxyApiBaseController {

    @Autowired
    private TeaMistakesKnowRepository teaMistakesKnowRepository;

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyKnowledgeRepository yxyKnowledgeRepository;

    @Autowired
    private TeaMistakesQuesRepository teaMistakesQuesRepository;

    @Autowired
    private YxyResUtil yxyResUtil;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/mistakesknow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model) {
        YxyProfile yxyProfile = getProfile(request, response);
        String teaId = yxyProfile.getId();
        List<MistakesKnow> mistakesKnowLst = new ArrayList<TeaMistakesKnowController.MistakesKnow>();
        // 根据章节获取知识点集
        List<String> bookcontentList = yxyResUtil.findTocByTocParent(model.bookContentId);

        if (CollectionUtils.isEmpty(bookcontentList)) {
            bookcontentList.add("123456");
        }
        List<YxyResBookContentsKnoledges> knowLst = yxyResBookContentsKnoledgesRepository
                .findByBookContentIdIn(bookcontentList);

        // 查询总错题数所需知识点id集
        List<Integer> knowIdLst = new ArrayList<Integer>();

        if (knowLst.size() > 0) {
            for (YxyResBookContentsKnoledges yrbcK : knowLst) {
                TeaMistakesKnow tmk = teaMistakesKnowRepository.findOne(model.classId + yrbcK.getKnowledgeId());
                MistakesKnow mk = new MistakesKnow();
                mk.knowName = yxyKnowledgeRepository.findOne(yrbcK.getKnowledgeId()).getName();
                if (tmk != null) {
                    mk.mistakesRate = tmk.getMistakesRate();
                }
                mistakesKnowLst.add(mk);
                knowIdLst.add(yrbcK.getKnowledgeId());
            }
        }
        // 进行排序
        mistakesKnowLst.sort((MistakesKnow h1, MistakesKnow h2) -> h2.mistakesRate.compareTo(h1.mistakesRate));

        Integer count = teaMistakesQuesRepository.countByClassIdAndPublishUserIdAndKnowIdLstIn(model.classId, teaId,
                knowIdLst);

        RespModel respModel = new RespModel();
        respModel.count = count;
        respModel.mistakesKnow = mistakesKnowLst;
        return respModel;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 所查询的班级id
        public String classId;

        // 教材章节id
        public String bookContentId;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // 错题数量
        public Integer count;
        // 知识点错误率
        public List<MistakesKnow> mistakesKnow;
    }

    /**
     * 知识点错误率
     * 
     */
    protected static class MistakesKnow {
        // 知识点
        public String knowName;

        // 错误比率
        public Double mistakesRate = 0.00;
    }
}
